-------------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  E:\REStat_MS14767_Vol96(2)\Data preparation Compustat segment\11_patentpanel.log
  log type:  text
 opened on:  19 Dec 2014, 21:48:42

. *-----------------------------------------------------------------
. 
. use "raw data\apat63_99.dta"

. so assignee

. drop if gyear<1969
(349072 observations deleted)

. 
. joinby assignee using "patent files\cusipnamemerged_red.dta", unmatched(both)

. 
. tab _merge 

                       _merge |      Freq.     Percent        Cum.
------------------------------+-----------------------------------
          only in master data |  1,960,613       75.37       75.37
both in master and using data |    640,872       24.63      100.00
------------------------------+-----------------------------------
                        Total |  2,601,485      100.00

. 
. keep if _merge==3
(1960613 observations deleted)

. drop _merge

. 
. *************************
. * Check observations
. count if assignee!=assignee[_n-1]
 3396

. * must give number around 3398
. ***************************
. 
. so ticker gyear

. drop if ticker==""
(0 observations deleted)

. 
. keep patent gyear assignee ticker 

. 
. compress
  ticker was str8 now str7
  (640,872 bytes saved)

. 
. gen t=1

. egen numpatentsy=sum(t), by (ticker gyear)

. 
. drop t patent assignee 

. 
. 
. gen year=1969

. save "patent files\patent1969.dta", replace
file patent files\patent1969.dta saved

. local i=69

. forvalues i=70/99 {
  2. replace year=19`i'
  3. save "patent files\patent19`i'.dta", replace
  4. }
(640872 real changes made)
file patent files\patent1970.dta saved
(640872 real changes made)
file patent files\patent1971.dta saved
(640872 real changes made)
file patent files\patent1972.dta saved
(640872 real changes made)
file patent files\patent1973.dta saved
(640872 real changes made)
file patent files\patent1974.dta saved
(640872 real changes made)
file patent files\patent1975.dta saved
(640872 real changes made)
file patent files\patent1976.dta saved
(640872 real changes made)
file patent files\patent1977.dta saved
(640872 real changes made)
file patent files\patent1978.dta saved
(640872 real changes made)
file patent files\patent1979.dta saved
(640872 real changes made)
file patent files\patent1980.dta saved
(640872 real changes made)
file patent files\patent1981.dta saved
(640872 real changes made)
file patent files\patent1982.dta saved
(640872 real changes made)
file patent files\patent1983.dta saved
(640872 real changes made)
file patent files\patent1984.dta saved
(640872 real changes made)
file patent files\patent1985.dta saved
(640872 real changes made)
file patent files\patent1986.dta saved
(640872 real changes made)
file patent files\patent1987.dta saved
(640872 real changes made)
file patent files\patent1988.dta saved
(640872 real changes made)
file patent files\patent1989.dta saved
(640872 real changes made)
file patent files\patent1990.dta saved
(640872 real changes made)
file patent files\patent1991.dta saved
(640872 real changes made)
file patent files\patent1992.dta saved
(640872 real changes made)
file patent files\patent1993.dta saved
(640872 real changes made)
file patent files\patent1994.dta saved
(640872 real changes made)
file patent files\patent1995.dta saved
(640872 real changes made)
file patent files\patent1996.dta saved
(640872 real changes made)
file patent files\patent1997.dta saved
(640872 real changes made)
file patent files\patent1998.dta saved
(640872 real changes made)
file patent files\patent1999.dta saved

. use "patent files\patent1969.dta", clear

. append using "patent files\patent1970.dta"

. append using "patent files\patent1971.dta"

. append using "patent files\patent1972.dta"

. append using "patent files\patent1973.dta"

. append using "patent files\patent1974.dta"

. append using "patent files\patent1975.dta"

. append using "patent files\patent1976.dta"

. append using "patent files\patent1977.dta"

. append using "patent files\patent1978.dta"

. append using "patent files\patent1979.dta"

. append using "patent files\patent1980.dta"

. append using "patent files\patent1981.dta"

. append using "patent files\patent1982.dta"

. append using "patent files\patent1983.dta"

. append using "patent files\patent1984.dta"

. append using "patent files\patent1985.dta"

. append using "patent files\patent1986.dta"

. append using "patent files\patent1987.dta"

. append using "patent files\patent1988.dta"

. append using "patent files\patent1989.dta"

. append using "patent files\patent1990.dta"

. append using "patent files\patent1991.dta"

. append using "patent files\patent1992.dta"

. append using "patent files\patent1993.dta"

. append using "patent files\patent1994.dta"

. append using "patent files\patent1995.dta"

. append using "patent files\patent1996.dta"

. append using "patent files\patent1997.dta"

. append using "patent files\patent1998.dta"

. append using "patent files\patent1999.dta"

. 
. save "patent files\raw_patent_panel.dta", replace
file patent files\raw_patent_panel.dta saved

. 
. 
. *************
. * generate patent dummy
. *************
. 
. gen pat=0

. replace pat=1 if year==gyear
(640872 real changes made)

. 
. so ticker year

. egen firmnum=group(ticker)

. 
. gen numpatents2=numpatentsy if pat==1
(19226160 missing values generated)

. 
. 
. *************
. * Reduce the blown up data to one row per company and year.
. * These commands finally create the patent panel.
. *************
. 
. so firmnum year

. count if firmnum==firmnum[_n-1] & year==year[_n-1]
19818548

. drop if firmnum==firmnum[_n-1] & year==year[_n-1] & pat==0
(19179205 observations deleted)

. drop if firmnum==firmnum[_n+1] & year==year[_n+1] & pat==pat[_n+1]
(617248 observations deleted)

. drop if firmnum==firmnum[_n+1] & year==year[_n+1] & pat==0 & pat[_n+1]==1
(22095 observations deleted)

. 
. drop gyear numpatentsy pat firmnum

. rename numpatents2 numpatentsy

. replace numpatentsy=0 if numpatentsy==.
(24860 real changes made)

. label var numpatentsy "number of patents per firm/year"

.  
. so ticker year

. 
. gen patents=numpatentsy

. by ticker: replace patents=numpatentsy+patents[_n-1]*(1-0.15) if year>1969
(37352 real changes made)

. label var patents "patentstock, depreciated"

. 
. by ticker: gen patents_t_1=patents[_n-1]
(1564 missing values generated)

. by ticker: gen patents_t_2=patents[_n-2]
(3128 missing values generated)

. by ticker: gen patents_t_3=patents[_n-3]
(4692 missing values generated)

. by ticker: gen patents_t_4=patents[_n-4]
(6256 missing values generated)

. label var patents_t_1 "patentstock, lag 1"

. label var patents_t_2 "patentstock, lag 2"

. label var patents_t_3 "patentstock, lag 3"

. label var patents_t_4 "patentstock, lag 4"

. 
. replace patents=0 if patents==.
(0 real changes made)

. replace patents_t_1=0 if patents_t_1==.
(1564 real changes made)

. replace patents_t_2=0 if patents_t_2==.
(3128 real changes made)

. replace patents_t_3=0 if patents_t_3==.
(4692 real changes made)

. 
. sort ticker year

. 
. save patentpanel.dta, replace
file patentpanel.dta saved

. 
. *log close
. 
. 
. 
. 
. 
end of do-file

. 
. ****************************************
. * Last step: Creation of final sample  *
. ****************************************
. 
. * This final step prepares the final sample using compustat_master_america.dta
. * and connecting it to the RJVvars from RJVvars.dta and links.dta
. * Patent infos are added and sales are corrected. Then, market shares are defined
. * and explaining variables are generated.
. * The final sample is saved as RESTATestimation_sample_segment.dta
. 
. 
. do 12_estimation_sample.do

. *---------------------------------------------------
. cap clear mata

. cap clear

. capture log close
